<?php

namespace app\queue\stomp;

use app\queue\BaseConsumer;
use common\model\MessageModel;

/**
 * 修改工单助手的最后一次信息时间
 */
class CardMessageFormat0628Consumer extends BaseConsumer
{

    // 要消费的队列名
    public $queue = 'card_message_format0628';

    // 连接名，对应 config/redis_queue.php 里的连接`
    public $connection = 'default';

    /**
     * @param $data
     */
    public function doConsume($data)
    {
        try {
            $messageList = model(MessageModel::class)->where(['id' => ['IN', $data['ids']]])->select();

            foreach ($messageList as $message) {
                $contentJson = json_decode($message['content'], true);
                $updateData = [];
                if (!isset($contentJson['tpl'])) {
                    $newContent = [
                        'tpl' => [
                            'type' => $contentJson['type'],
                            'buttons' => [],
                            'content_fields' => [
                            ],
                        ],
                        'type' => $contentJson['type'],
                        'tpl_data' => [
                            'title' => $contentJson['title']
                        ],
                        'message_key' => $contentJson['message_key'] ?? 'unknown',
                        'format_processed' => 'yes'
                    ];
                    $tempStrList = explode('<br/>', $contentJson['content']);
                    foreach ($tempStrList as $index => $tempStr) {
                        if ($index == 0) {
                            $newContent['tpl_data']['first'] = $tempStr;
                            continue;
                        }
                        $strExplode = explode('：', $tempStr);
                        if (count($strExplode) > 1) {
                            list($label, $value) = explode('：', $tempStr);
                            $paramName = md5($label);
                            $newContent['tpl']['content_fields'][] = [
                                'label' => $label,
                                'param_name' => $paramName,
                            ];
                            $newContent['tpl_data'][$paramName] = $value;
                        } else {
                            $newContent['tpl_data']['remark'] = $tempStr;
                        }
                    }
                    $updateData['content'] = $newContent;
                }


                switch ($contentJson['type']) {
                    case 'examine':
                    case 'approval':
                        $notifyType = $contentJson['type'];
                        break;
                    case 'todo':
                    case 'notify_task_member':
                    case 'not_started_task_statistics_daily':
                        $notifyType = 'todo';
                        break;
                    case 'task_remind':
                    case 'demand':
                    case 'remind':
                    case 'mark_read':
                    default:
                        $notifyType = 'task_remind';
                }
                if ($notifyType != $message['notify_type']) {
                    $updateData['notify_type'] = $notifyType;
                }

                if (empty($updateData)) {
                    continue;
                }
                $updateData['id'] = $message['id'];
                model(MessageModel::class)->modifyItem($updateData);
            }

        } catch (\Throwable $e) {
            trace(format_exception_info($e), 'ERR');
        }
    }
}
